#ifndef __CONFIG_H__
#define __CONFIG_H__

#include <stdbool.h>
#include <stdint.h>

#define COMPENSTAION_ID  0x7df // 温湿度补偿系数的特殊CAN ID
#define ERASURE_ID       0x7df // 擦除Flash补偿系数的特殊CAN ID
#define FLASH_WRITE_FLAG 0x23  // FLASH中是否写入补偿系数标志

/* 补偿系数最大值 */
#define MAX_COMPENSTAION_VALUE 1.0f

/* 补偿值有效范围 */
#define MAX_VALID_VALUE 650000
#define MIN_VALID_VALUE 100

/* NODE_ID 最大值 */
#define NODE_ID_MAX 15

/* 传感器点数最大数量 */
#define SENSOR_POINT_MAXSIZE 14

/* 鍙傝€冪偣鏃犳晥鍊? */
#define COMP_REF_POINT_INVALID_VALUE 0xff

/* 补偿点或参考点最大数量 */
#define COMP_AND_REF_POINT_MAXSIZE 2

/* 补偿配置详细存储的数量*/
#define COMP_CONFIG_INFO_COUNT 8

/* 补偿配置写入FLAG */
#define COMP_CONFIG_WRITE_FLAG 0x66

/* 湿补点配置数据POS */
#define COMP_CONFIG_POINT_NUM_POS        0
#define COMP_CONFIG_START_SER_NUM_POS    1
#define COMP_CONFIG_COMP_NUM_POS         2
#define COMP_CONFIG_COMP_POINT_1_POS     3
#define COMP_CONFIG_COMP_REF_POINT_1_POS 4
#define COMP_CONFIG_COMP_POINT_2_POS     5
#define COMP_CONFIG_COMP_REF_POINT_2_POS 6
#define COMP_CONFIG_FLAG_POS             7

/* CAN加密基ID */
#define CAN_ENCRYPT_BASE_ID 0x320

/* 传感器点数最大值 */
#define SENSOR_POINT_MAX_NUM 12

typedef struct
{
    /* 补偿点初始值 */
    uint16_t initialValue;

    /* 补偿点当前值 */
    uint32_t currentValue;

    /* 补偿系数 =  1.0 * initialValue / currentValue */
    float coefficient;

} Compensation_t;

/* LK1A 和 PK1B 节点ID */
typedef enum
{
    /* LK1A 节点ID */
    NODE_ID_LK1A_LEG    = 3,
    NODE_ID_LK1A_LUMBAR = 4,
    NODE_ID_LK1A_RIGHT  = 6,
    NODE_ID_LK1A_LEFT   = 7,

    /* PK1B 节点ID */
    NODE_ID_PK1B_BACK   = 1,
    NODE_ID_PK1B_LEG    = 2,
    NODE_ID_PK1B_LUMBAR = 5,
    NODE_ID_PK1B_RIGHT  = 8,
    NODE_ID_PK1B_LEFT   = 9,

} Node_ID_e;

/* 传感器类型 LK1A or PK1B */
typedef enum
{
    SENSOR_TYPE_LK1A,
    SENSOR_TYPE_PK1B,
    SENSOR_TYPE_UNKNOWN
} Sensor_Type_e;

/* 补偿点位 0开始 */
typedef enum
{
    /* LK1A补偿点位 */
    COMP_POINT_LK1A_LEG_L  = 1 - 1,
    COMP_POINT_LK1A_LEG_R  = 12 - 1,
    COMP_POINT_LK1A_LUMBAR = 4 - 1,
    COMP_POINT_LK1A_RIGHT  = 2 - 1,
    COMP_POINT_LK1A_LEFT   = 5 - 1,

    /* PK1B 补偿点位 */
    COMP_POINT_PK1B_BACK   = 10 - 1,
    COMP_POINT_PK1B_LEG_L  = 3 - 1,
    COMP_POINT_PK1B_LEG_R  = 10 - 1,
    COMP_POINT_PK1B_LUMBAR = 10 - 1,
    COMP_POINT_PK1B_LEFT   = 3 - 1,
    COMP_POINT_PK1B_RIGHT  = 3 - 1
} Comp_Point_e;

/* 补偿参考点位 */
typedef enum
{
    COMP_REF_POINT_LK1A_LEG_L  = 2 - 1,
    COMP_REF_POINT_LK1A_LEG_R  = 11 - 1,
    COMP_REF_POINT_LK1A_LUMBAR = 5 - 1,
    COMP_REF_POINT_LK1A_RIGHT  = 1 - 1,
    COMP_REF_POINT_LK1A_LEFT   = 6 - 1,

    // TODO PK1B
    COMP_REF_POINT_PK1B_BACK   = 11 - 1,
    COMP_REF_POINT_PK1B_LEG_L  = 4 - 1,
    COMP_REF_POINT_PK1B_LEG_R  = 11 - 1,
    COMP_REF_POINT_PK1B_LUMBAR = 8 - 1,
    COMP_REF_POINT_PK1B_LEFT   = 1 - 1,
    COMP_REF_POINT_PK1B_RIGHT  = 1 - 1,

} Comp_Ref_Point_e;

/* 传感器点位区间 [min, max] */
typedef struct
{
    uint8_t min;

    uint8_t max;
} Sensor_Point_Section_t;

typedef struct
{
    /* 是否通过CAN配置湿补点 */
    bool is_config;

    /* 点位数 */
    uint8_t point_num;

    /* 起始点位号 */
    uint8_t start_num;

    /* 湿补点对数 */
    uint8_t comp_point_count;

    /* 湿补点数组 */
    uint8_t comp_point_arr[COMP_AND_REF_POINT_MAXSIZE];

    /* 参考点数组 */
    uint8_t ref_point_arr[COMP_AND_REF_POINT_MAXSIZE];

    /* flag */
    uint8_t flag;

    /* 传感器点位号区间 */
    Sensor_Point_Section_t sensor_point_section;

} Comp_Config_t;

/* 传感器点位信息 */
typedef struct
{
    /* 是否是补偿点 */
    bool is_comp_point;

    /* 补偿参考点 */
    uint8_t comp_ref_point;

    /* 传感器类型 */
    Sensor_Type_e sensor_type;

    /* node id, 0x31X - 0x310 */
    uint8_t node_id;

    /* 补偿点配置 */
    Comp_Config_t comp_config;

} sensor_point_info_t;

/*   compensation */
extern Compensation_t compensation;

/*  是否开启 compensation */
extern bool is_comp_en;

/*  是否接收到湿补点初始值 */
extern bool is_recv_comp_init_val;

/*  是否擦除Flash标志位 */
extern bool is_recv_erasure_flash;

/* 是否数据加密标志位 */
extern bool is_data_encrypt;

/* AES 密钥 和 IVR */
extern uint8_t aes_key_buf[16];
extern uint8_t aes_ivr_buf[16];

#endif
